package com.example.yzj16478026;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class OrderActivity extends Activity {
    private CheckBox itm_xcr,itm_hsdf,itm_djt,itm_xbc,itm_hsyk,itm_pjy;
    private Button sub_order;
    private RadioButton use;
    private EditText orderid,addr;
    private int totalCount;
    private ArrayList<String> nameList=new ArrayList<String>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        init();
        bindEvent();
    }

    private CompoundButton.OnCheckedChangeListener onclick(final int price, final String name){
        return new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    totalCount+=price;
                    nameList.add(name);
                    System.out.println(totalCount);
                    System.out.println(nameList);
                }else {
                    totalCount-=price;
                    nameList.remove(name);
                    System.out.println(nameList);
                    System.out.println(totalCount);
                }
            }
        };
    }

    private void bindEvent() {
        use.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    orderid.setVisibility(View.VISIBLE);
                }else {
                    orderid.setVisibility(View.GONE);
                }
            }
        });
        itm_xbc.setOnCheckedChangeListener(onclick(6,"小白菜"));
        itm_xcr.setOnCheckedChangeListener(onclick(30,"小炒肉"));
        itm_hsdf.setOnCheckedChangeListener(onclick(8,"红烧豆腐"));
        itm_djt.setOnCheckedChangeListener(onclick(20,"大鸡腿"));
        itm_hsyk.setOnCheckedChangeListener(onclick(15,"红烧鱼块"));
        itm_pjy.setOnCheckedChangeListener(onclick(16,"啤酒鸭"));
        sub_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(OrderActivity.this,OrderResp.class);
                intent.putStringArrayListExtra("nameList", nameList);
                intent.putExtra("totalCount",totalCount);
                intent.putExtra("addr",addr.getText().toString());
                startActivity(intent);
                totalCount=0;
//                nameList.clear();
            }
        });
    }

    private void init() {
        itm_xcr=findViewById(R.id.itm_xcr);
        itm_djt=findViewById(R.id.itm_djt);
        itm_hsdf=findViewById(R.id.itm_hsdf);
        itm_hsyk=findViewById(R.id.itm_hsyk);
        itm_pjy=findViewById(R.id.itm_pjy);
        itm_xbc=findViewById(R.id.itm_xbc);
        use=findViewById(R.id.use);
        sub_order=findViewById(R.id.sub_order);
        orderid=findViewById(R.id.orderid);
        addr=findViewById(R.id.addr);
    }
}
